package com.example.mobile2021606159yhf0408;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    //声明控件变量
    private TextView tvmianTitle,tvmainTip,tvmianResule;
    private CheckBox chkboxMainYinqing,chkboxMainBoli,chkboxMainFangxiangpan;
    private Button btnMainBuy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //调用initView（）方法
        initView();

        chkboxMainYinqing.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //根据控件的状态，在文本中显示对应信息
                if (isChecked) {
                    tvmianResule.setText("选中：" + buttonView.getText().toString());
                } else {
                    tvmianResule.setText("取消选中：" + buttonView.getText().toString());
                }
            }
        });

        chkboxMainFangxiangpan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //根据控件的状态，在文本中显示对应信息
                if (isChecked) {
                    tvmianResule.setText("选中:" + buttonView.getText().toString());
                } else {
                    tvmianResule.setText("取消选中:" + buttonView.getText().toString());
                }
            }
        });
        //添加按钮点击事件监听器
        btnMainBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View V) {

            }
        });
        //添加按钮点击事件监听器
        btnMainBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String msg="选中：";
                //依次判断复选框的选中状态
                if (chkboxMainYinqing.isChecked()) {
                    msg+="\n"+chkboxMainYinqing.getText().toString();
                }
                tvmianResule.setText(msg);
            }
        });
        //添加按钮点击事件监听器
        btnMainBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String msg="选中:";
                //依次判断复选框的选中状态
                if (chkboxMainYinqing.isChecked()){
                    msg+="\n"+chkboxMainYinqing.getText().toString();
                }
                if (chkboxMainBoli.isChecked()){
                    msg+="\n"+chkboxMainBoli.getText().toString();
                }
                if (chkboxMainFangxiangpan.isChecked()){
                    msg+="\n"+chkboxMainFangxiangpan.getText().toString();
                }
                tvmianResule.setText(msg);
            }
        });
    }
    //定义initView（）方法，调用findViewById方法通过ID获取空间
    public void initView(){
        tvmianTitle = (TextView) findViewById(R.id.tv_mian_tip);
        chkboxMainYinqing = (CheckBox) findViewById(R.id.Chkbox_main_yinqing);
        chkboxMainBoli = (CheckBox) findViewById(R.id.Chkbox_main_boli);
        chkboxMainFangxiangpan = (CheckBox) findViewById(R.id.Chkbox_main_fangxiangpan);
        btnMainBuy = (Button) findViewById(R.id.Btn_main_buy);
        tvmianResule = (TextView) findViewById(R.id.TV_nain_result);
    }

}